﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>

 <script src="http://maps.googleapis.com/maps/api/js?sensor=false&amp;libraries=places"></script>

    <script type="text/javascript" src="<%=Url.Content("~/Scripts/jquery.geocomplete.js")%>"></script>

    <script type="text/javascript">

        $(document).ready(function() {
            function getAddressFromLatLang(lat, lng) {

                var geocoder = new google.maps.Geocoder();
                var latLng = new google.maps.LatLng(lat, lng);
                geocoder.geocode({ 'latLng': latLng }, function(results, status) {

                    if (status == google.maps.GeocoderStatus.OK) {
                        if (results[0]) {

                              $("#geocomplete").val( (results[0].formatted_address));
                               $("#find").trigger("click");
                        }
                    } else {
                        alert("Geocode was not successful    for the following reason: " + status);
                    }
                });
            }
            //
            var latitude = '<%=(ViewData["Latitude"] != null) ? ViewData["Latitude"].ToString() : ""%>';
            var longitude = '<%=(ViewData["Longitude"] != null) ? ViewData["Longitude"].ToString() : ""%>';
            $("#geocomplete").geocomplete({
                map: "#map_canvas",
                details: "form ",
                markerOptions: {
                    draggable: true
                }
            });

            $("#geocomplete").bind("geocode:dragged", function(event, latLng) {
                $("input[name=lat]").val(latLng.lat());
                $("input[name=lng]").val(latLng.lng());
                //$("#reset").show();
            });


            $("#reset").click(function() {
                $("#geocomplete").geocomplete("resetMarker");
                //  $("#reset").hide();
                return false;
            });
            $("#find").click(function() {
                $("#geocomplete").trigger("geocode");
            }).click();
            //
            if (latitude != "" && longitude != "") {
                getAddressFromLatLang(latitude,longitude);          
                
            }

        });
        
       
    </script>